// pages/speciallist/speciallist.js
import http from '../../utils/request'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page:1,
        pageSize:10,
        levelId:'', //专题等级
        stateId:'', //专题状态
        leixingId:'', //专题类型
        timeId:'', //适应时间
        areaId:'', //适应地区
        projectItem: [],
        isHideLoadMore:true,
        moreRequest:true,//控制用户手贱多次下拉触发请求
        parms:"",//查询条件参数用于下拉滚动
        speciallist:[],
        //专题等级
        level:[],
        //专题状态
        state:[
            {id:0,name:'更新中'},
            {id:1,name:'已完结'}
        ],
        //专题类型
        leixing:[],
        //适应时间
        time:[],
        //适应地区
        area:[],
        xueke:[],
        selectIsshow:false,
        activeindex:-1,
        activeindex1:-1,
        activeindex2:-1,
        activeindex3:-1,
        activeindex4:-1,
        activeindex5:-1,
        showTop:false,
        classifyList:[],
        activeHover:0,
        videotypeId:'2',
        videotype2Id:'',
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      console.info(options)
      this.setData({leixingId:options.id?options.id:undefined})
        // this.data.leixingId = options.id?options.id:undefined
        this.clearCache();//清本页缓存
        this.getArticles(1);//第一次加载数据
        this.getSpecialLevel() // 专题类型一级
        this.getSpecialLevelSecond(2)
        this.resouceLevel() //资源等级
        // this.getSpecial()
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.clearCache();
        this.getArticles(1);//第一次加载数据
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let {page,loadMore}=this.data
        let that = this
        if (!loadMore) {
            that.setData({
                loadMore: true, //加载中  
                loadAll: false //是否加载完所有数据
            });
            //加载更多，这里做下延时加载
            setTimeout(function() {
                that.getArticles(page);
            }, 1000)
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
         // 获取加载的页面对象
        const pages = getCurrentPages();
        // 获取当前页面的对象
        const currentPage = pages[pages.length - 1];
        console.info(currentPage)
        let url= currentPage.route
      let  allurl = url
        return {
          path:allurl
        }
    },
    //筛选条件显示
    shaixuanClick(){
        this.setData({selectIsshow:true})
        // this.specialLevel()//专题等级
        // this.getSpecialType() //专题类型
        this.getTime() //适应时间
        this.getArea() //适应地区
        this.getXueke() //学科
    },
    // 清缓存
   clearCache:function(){
        this.setData({
            page:1,
            speciallist: [] //文章列表数组清空
        }); 
    },
   async getArticles () {
        //设置默认值
        var that = this;
        var {page,pageSize,stateId,leixingId,timeId,areaId,videotypeId,videotype2Id,stringset,levelId} = this.data;
        var options={
           url:'/getItemCaseByName',
           method:'POST',
           data:{
                page:page,
                pagesize:pageSize,
                videotype2Id:leixingId?leixingId:undefined,
                subjectId:areaId?areaId:undefined,
                labelitemcaseId:timeId?timeId:undefined,
                type:'专题',
                rbtype:stateId?stateId:undefined,
                videotypeId:videotypeId?videotypeId:undefined,
                // videotype2Id:videotype2Id?videotype2Id:undefined,
                sqlwhere:stringset?stringset:'',
                themetypeId:levelId?levelId:undefined,
            },
            header:{"content-type":"application/json;charset=UTF-8"}
        }
        let res = await http(options)
        if (res.data.code == 0) {//成功
            if (res.data.obj && res.data.obj.resultRBs.length > 0) {
                res.data.obj.resultRBs.map(item=>{item.neRbname = item.rbName.replace(/\<br>/g,"")})
                let {page,speciallist,pageSize} = that.data
                page++
                // 这一步实现了上拉加载更多
                let tmpArr = speciallist.concat(res.data.obj.resultRBs)
                that.setData({
                    speciallist: tmpArr,
                    page:page,
                    loadMore: false
                })
                if (res.data.obj.resultRBs.length < pageSize) {
                    that.setData({
                      loadMore: false, //隐藏加载中。。
                      loadAll: true //所有数据都加载完了
                    });
                  }
            }else{
                that.setData({
                    loadAll: true, //把“没有数据”设为true，显示  
                    loadMore: false //把"上拉加载"的变量设为false，隐藏  
                  });
            }
        } else {//失败
            console.log(res.data.info);
        }  
     },
     //专题等级
    // async specialLevel(){
    //     var that = this;
    //     var options={
    //        url:'/getThemetype',
    //        method:'POST',
    //        header:{"content-type":"application/json;charset=UTF-8"}
    //     }
    //     let res = await http(options)
    //     if(res.data.code==0){
    //         that.setData({
    //             level: res.data.obj
    //         });
    //     }
    // },
    //专题一级类型
    async getSpecialLevel(){
        var that = this;
        let newNianji=[]
        var options={
           url:'/getVideotype',
            method:'POST',
        }
        let res = await http(options)
        if(res.data.code==0){
            newNianji = res.data.obj.filter(item=>item.videotypename!='同步视频'&&item.videotypename!='样书')
            this.setData({classifyList:newNianji})
        }
    },
     //专题二级类型
     async getSpecialLevelSecond(id){
        var that = this;
        let newNianji=[]
        var options={
           url:'/getVideotype2',
            method:'POST',
            data:{videotypeId:id},
            header:{"content-type":"application/json;charset=UTF-8"}
        }
        let res = await http(options)
        if(res.data.code==0){
            this.setData({leixing:res.data.obj})
        }
    },
    //专题类型
    // async getSpecialType(){
    //     var that = this;
    //     var options={
    //        url:'/getlanguage',
    //         method:'POST',
    //     }
    //     let res = await http(options)
    //     if(res.data.code==0){
    //         that.setData({
    //             leixing: res.data.obj
    //         });
    //     }
    // },
    //适应时间
    async getTime(){
        var that = this;
        var options={
           url:'/getLabelitemcase',
            method:'POST',
        }
        let res = await http(options)
        if(res.data.code==0){
          res.data.obj.map(item=>item.nameNumber=parseInt(item.name))
          res.data.obj.sort((a, b) => a.nameNumber - b.nameNumber)
          that.setData({time:res.data.obj.reverse()})
        }
    },
    //适应地区
    async getArea(){
        var that = this;
        var options={
           url:'/getSubject',
            method:'POST',
        }
        let res = await http(options)
        if(res.data.code==0){
            that.setData({
                area: res.data.obj
            });
        }
    },
    // 专题等级点击
    // levelClick(e){
    //     let name = e.currentTarget.dataset.name
    //     let index = e.currentTarget.dataset.activeindex
    //     if(name=='不限'){
    //         this.setData({levelId:''})
    //     }else{
    //         this.setData({levelId:name})
    //     }
    //     this.setData({activeindex:index})
    // },
    // 专题状态点击
    stateClick(e){
        let name = e.currentTarget.dataset.name
        let index = e.currentTarget.dataset.activeindex1
        if(name=='不限'){
            this.setData({stateId:''})
        }else{
            this.setData({stateId:name})
        }
        this.setData({activeindex1:index})
    },
    // 专题类型点击
    leixingClick1(e){
        console.info(e)
        let name = e.currentTarget.dataset.name
        let index = e.currentTarget.dataset.activeindex2
        if(name=='不限'){
            this.setData({videotype2Id:''})
        }else{
            this.setData({videotype2Id:name})
        }
        this.setData({activeindex2:index})
    },
    // 适应时间点击
    timeClick(e){
        let name = e.currentTarget.dataset.name
        let index = e.currentTarget.dataset.activeindex3
        if(name=='不限'){
            this.setData({timeId:''})
        }else{
            this.setData({timeId:name})
        }
        this.setData({activeindex3:index})
    },
    // 适应地区点击
    areaClick(e){
        let name = e.currentTarget.dataset.name
        let index = e.currentTarget.dataset.activeindex4
        if(name=='不限'){
            this.setData({areaId:''})
        }else{
            this.setData({areaId:name})
        }
        this.setData({activeindex4:index})
    },
    //等级
    async resouceLevel(){
        let that = this
        let options={
            url:'/getThemetype',
            method:'POST'
        }
        let res = await http(options)
        that.setData({level:res.data.obj})
    },
    //专题分类
    specialHover(e){
        let index=e.currentTarget.dataset.index
        let id = e.currentTarget.dataset.id
        this.setData({activeHover:index,videotypeId:id,selectIsshow:false,page:1,speciallist:[],levelId:'',stateId:'',leixingId:'',timeId:'',areaId:'',videotype2Id:''})
        this.getArticles(1)
        this.getSpecialLevelSecond(id)
    },
    //适应学科
    async getXueke(){
        var that = this;
        let id = wx.getStorageSync('mhName')?wx.getStorageSync('mhName').id:11739
        let newXuek = []
        var options={
           url:'/getNavs5',
            method:'POST',
            data:{id:id},
            header:{"content-type":"application/json;charset=UTF-8"}
        }
        let res = await http(options)
        newXuek = res.data.filter(item=>item.name!='不限')
        that.setData({
            xueke: newXuek
        });
    },
    //专题学科
    xuekeClick(e){
        let name = e.currentTarget.dataset.name
        let index = e.currentTarget.dataset.activeindex5
        if(name=='不限'){
            this.setData({stringset:''})
        }else{
            this.setData({stringset:`and n2='${name}'`})
        }
        this.setData({activeindex5:index})
    },
    // 重置
    reset(){
        this.setData({activeindex:-1,activeindex1:-1,activeindex2:-1,activeindex3:-1,activeindex4:-1,levelId:'',stateId:'',leixingId:'',timeId:'',areaId:'',videotypeId:'',videotype2Id:'',sqlwhere:''})
    },
    // 确定
    sure(){
        this.setData({selectIsshow:false,page:1,speciallist:[]})
        this.getArticles(1)
    },
    onPageScroll: function (e) {
        if (e.scrollTop>300) {
          this.setData({
            showTop: false
          })
        } else {
          this.setData({
            showTop: true
          })
        }
      },
      goTop: function () {
        wx.pageScrollTo({
          scrollTop: 0,
          duration: 300
        })
      },
      linkRouter(e){
        let title=e.currentTarget.dataset.name
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: `/pages/specialdetail/specialdetail?title=${title}&id=${id}`
        });
    },
    // 替换换行
		replaceBr(s1){
			// return s1.replace('<br />','')
			return s1.replace(/\<br>/g,"")
        },
        // 资源等级
    leixingClick(e){
        let name = e.currentTarget.dataset.name
        let index = e.currentTarget.dataset.activeindex6
        if(name=='不限'){
            this.setData({levelId:''})
        }else{
            this.setData({levelId:name})
        }
        this.setData({activeindex6:index})
    },
})